package com.youyou.fusheng2.View.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.youyou.fusheng2.Moudle.OnClick2;
import com.youyou.fusheng2.Moudle.sousuoShow;
import com.youyou.fusheng2.R;

import java.util.List;
import java.util.Map;

/**
 * @name fusheng2
 * @class name：com.youyou.fusheng2.View.Adapter
 * @anthor 浮生 QQ:2567294605
 * @time 2020/8/17 18:20
 * @to
 */
public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int FLAGEZREO = 0;
    private static final int FLAGEONE = 1;
    private static final int FLAGETWO = 2;
    private static final int FLAGETHRE = 3;
    private List<String> bannerTests;
    private List<String> bannerTestss;
    private List<Map<String, String>> typeTests;
    private List<Map<String, String>> likeTests;
    private OnClick2 onClick2;
    private sousuoShow sousuoShow;

    public HomeAdapter(sousuoShow sousuoShow, OnClick2 onClick2) {
        this.sousuoShow = sousuoShow;
        this.onClick2 = onClick2;
    }

    public void setBannerTests(List<String> bannerTests, List<String> bannerTestss) {
        this.bannerTests = bannerTests;
        this.bannerTestss = bannerTestss;
    }

    public void setTypeTests(List<Map<String, String>> typeTests) {
        this.typeTests = typeTests;
    }

    public void setLikeTests(List<Map<String, String>> likeTests) {
        this.likeTests = likeTests;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == FLAGEZREO) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_banner, parent, false);
            return new HomeBanner(inflate);
        }
        if (viewType == FLAGEONE) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_radiobutton, parent, false);
            return new HomeRadio(inflate);
        }
        if (viewType == FLAGETWO) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_listbanner, parent, false);
            return new HomeListBanners(inflate);
        }
        if (viewType == FLAGETHRE) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_imagelist, parent, false);
            return new HomeImage(inflate);
        }
        return null;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HomeBanner) {
            ((HomeBanner) holder).home_banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            ((HomeBanner) holder).home_banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(context)
                            .load(path)
                            .into(imageView);
                }
            });
            ((HomeBanner) holder).home_banner.setImages(bannerTests);
            ((HomeBanner) holder).home_banner.setDelayTime(2000);
            ((HomeBanner) holder).home_banner.start();
            ((HomeBanner) holder).home_banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    /**
                     *  @作者: $浮生-qq:2567294605$
                     *  @时间: 2020/8/29  16:05
                     *  @简介: 参数1是标记
                     *  参数2 是回掉的内容
                     */
                    onClick2.listener1(0, bannerTestss.get(position));
                }
            });
        }
        if (holder instanceof HomeRadio) {
            setClick(((HomeRadio) holder).main_home, 0);
            setClick(((HomeRadio) holder).mian_fenlei, 1);
            setClick(((HomeRadio) holder).main_sousuo, 2);
            setClick(((HomeRadio) holder).main_mine, 3);
        }

        if (holder instanceof HomeListBanners) {
            HomeListsAdapter homeListAdapter = new HomeListsAdapter();
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(holder.itemView.getContext(), LinearLayoutManager.HORIZONTAL, false);
            ((HomeListBanners) holder).home_banner.setLayoutManager(layoutManager);
            ((HomeListBanners) holder).home_banner.setAdapter(homeListAdapter);
            homeListAdapter.setListTests(typeTests);
        }

        if (holder instanceof HomeImage) {
            ((HomeImage) holder).imageView.setBackgroundColor(R.color.homeList);
            Glide.with(holder.itemView.getContext())
                    .load(likeTests.get(position - 3).get("pic"))
                    .into(((HomeImage) holder).imageView);
            ((HomeImage) holder).home_text.setText(likeTests.get(position - 3).get("tname"));
            ((HomeImage) holder).imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    /**
                     *  @作者: $浮生-qq:2567294605$
                     *  @时间: 2020/8/29  16:05
                     *  @简介: 参数1是标记
                     *  参数2 是回掉的内容
                     */
                    onClick2.listener1(1, likeTests.get(position - 3).get("tid"));
                }
            });
        }
    }

    private void setClick(RadioButton button, int i) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  01:44
         *  @简介: 四个button的点击事件
         *  第一个参数是标记
         *  第二个参数是获取radioButton的文字
         *  第三个参数是点击跳转到另一个页面加载哪个tab
         */
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClick2.listener1(0, button.getText().toString(), i);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return FLAGEZREO;
        } else if (position == 1) {
            return FLAGEONE;
        } else if (position == 2) {
            return FLAGETWO;
        } else if (position == 3) {
            return FLAGETHRE;
        } else {
            return FLAGETHRE;
        }

    }

    @Override
    public int getItemCount() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/20  19:06
         *  @简介: 如果bannerTests为null就设置为0个item
         */
        return bannerTests == null ? 0 : likeTests.size() + 3;
    }


    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/20  19:06
         *  @简介: 重写此方法 以达到 顶部三哥固定的item 不会交错
         */
        super.onViewAttachedToWindow(holder);
        int adapterPosition = holder.getAdapterPosition();
        if ((getItemViewType(adapterPosition) < 3)) {
            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
            if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams p =
                        (StaggeredGridLayoutManager.LayoutParams) lp;
                p.setFullSpan(true);
            }
        }
    }

    private static class HomeBanner extends RecyclerView.ViewHolder {
        /**
         * @作者: $浮生-qq:2567294605$
         * @时间: 2020/8/17  18:25
         * @简介: 轮播图viewHoder
         */

        private Banner home_banner;

        public HomeBanner(View inflate) {
            super(inflate);
            home_banner = inflate.findViewById(R.id.home_banner);
        }
    }

    private static class HomeRadio extends RecyclerView.ViewHolder {
        /**
         * @作者: $浮生-qq:2567294605$
         * @时间: 2020/8/17  18:25
         * @简介: 四个button的viewHoder
         */
        private RadioButton main_home;
        private RadioButton mian_fenlei;
        private RadioButton main_sousuo;
        private RadioButton main_mine;
        private RadioGroup main_group;

        public HomeRadio(View inflate) {
            super(inflate);
            main_home = inflate.findViewById(R.id.main_home);
            mian_fenlei = inflate.findViewById(R.id.mian_fenlei);
            main_sousuo = inflate.findViewById(R.id.main_sousuo);
            main_mine = inflate.findViewById(R.id.main_mine);
            main_group = inflate.findViewById(R.id.main_group);
        }
    }

    private static class HomeListBanners extends RecyclerView.ViewHolder {
        /**
         * @作者: $浮生-qq:2567294605$
         * @时间: 2020/8/17  18:26
         * @简介: 横向滑动的viewHoder
         */
        private RecyclerView home_banner;

        public HomeListBanners(View inflate) {
            super(inflate);
            home_banner = inflate.findViewById(R.id.home_list);
        }
    }

    private static class HomeImage extends RecyclerView.ViewHolder {
        /**
         * @作者: $浮生-qq:2567294605$
         * @时间: 2020/8/17  18:26
         * @简介: home页面底部每个item的控件
         */
        ImageView imageView;
        TextView home_text;

        public HomeImage(View inflate) {
            super(inflate);
            imageView = inflate.findViewById(R.id.home_image);
            home_text = inflate.findViewById(R.id.home_text);

            /**
             *  @作者: $浮生-qq:2567294605$
             *  @时间: 2020/8/20  19:05
             *  @简介: 设置每个item的高度 实现瀑布流布局
             */
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.height = (int) (300 + Math.random() * 400);
            imageView.setLayoutParams(layoutParams);
        }
    }

}
